package com.xinruke.hostels.reservation.service;

import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwZhengjianxinxi;
import com.xinruke.hostels.common.po.constants.YwZhengjianxinxiConstants;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.reservation.mapper.YwZhengjianxinxiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @ClassName GuestService
 * @CreateDate 2019/7/9
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 宾客Service
 */
@Service
public class GuestService {
    @Autowired
    YwZhengjianxinxiMapper zhengjianxinxiMapper;

    /**
     * 保存宾客信息，有则更新，无则新增
     *
     * @param zhengjianxinxi
     * @return
     */
    public int saveGuest(YwZhengjianxinxi zhengjianxinxi) {
        QueryConditions queryConditions = new QueryConditions();
        queryConditions.addCondition("jiudianId", EConditionOperator.EQULS, zhengjianxinxi.getJiudianId());
        queryConditions.addCondition("zhengjianleixing", EConditionOperator.EQULS, zhengjianxinxi.getZhengjianleixing());
        queryConditions.addCondition("zhengjianhao", EConditionOperator.EQULS, zhengjianxinxi.getZhengjianhao());
        queryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        MyExample myExample = new MyExample(YwZhengjianxinxi.class, queryConditions);
        List<YwZhengjianxinxi> zhengjianxinxiList = zhengjianxinxiMapper.selectByExample(myExample);
        if (CollectionUtils.isEmpty(zhengjianxinxiList)) {
            zhengjianxinxi.setChuangjianshijian(new Date());
            zhengjianxinxi.setGengxinshijian(new Date());
            zhengjianxinxi.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
            zhengjianxinxi.setZhuangtai(YwZhengjianxinxiConstants.ZHUANGTAI_QIYONG);

            int ret = zhengjianxinxiMapper.insertSelective(zhengjianxinxi);
            if (ret > 0) {
                return zhengjianxinxi.getId();
            } else {
                return ret;
            }
        } else {
            YwZhengjianxinxi zhengjianxinxi0 = zhengjianxinxiList.get(0);
            zhengjianxinxi0.setShoujihao(zhengjianxinxi.getShoujihao());
            zhengjianxinxi0.setXingming(zhengjianxinxi.getXingming());
            zhengjianxinxi0.setZhengjianhao(zhengjianxinxi.getZhengjianhao());
            zhengjianxinxi0.setZhengjianleixing(zhengjianxinxi.getZhengjianleixing());
            zhengjianxinxi0.setJiudianId(zhengjianxinxi.getJiudianId());
            zhengjianxinxiMapper.updateByPrimaryKeySelective(zhengjianxinxi0);
            return zhengjianxinxi0.getId();
        }
    }
}
